package com.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.system.pojo.Item;
import com.system.pojo.Vendor;
import com.system.service.RelationVendorItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RelationVendorItemController {
    @Autowired
    private RelationVendorItemService relationVendorItemService;

    @RequestMapping("selectVendorForPage")
    @ResponseBody
    public String selectVendorForPage(Vendor vendor, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int limit) {
        List<Vendor> list = relationVendorItemService.selectVendor(vendor);
        List<Vendor> people = relationVendorItemService.selectVendorByPage(vendor, page, limit);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("count", list.size());
        jsonObject.put("data", people);
        jsonObject.put("msg", "查询成功");
        return jsonObject.toJSONString();
    }

    @RequestMapping("selectItemForVendor")
    @ResponseBody
    public String selectItemForVendor(Vendor vendor, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int limit) {
        List<Item> list = relationVendorItemService.selectItem(vendor);
        List<Item> people = relationVendorItemService.selectItemByVendor(vendor, page, limit);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("count", list.size());
        jsonObject.put("data", people);
        jsonObject.put("msg", "查询成功");
        return jsonObject.toJSONString();
    }

    @RequestMapping("delItemForVendor")
    @ResponseBody
    public String delItemForVendor(String vendorId, String itemId) {
        JSONObject jsonObject = new JSONObject();
        try {
            relationVendorItemService.delItemForVendor(vendorId, itemId);
            jsonObject.put("code", 0);
            jsonObject.put("msg", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "删除失败");
        }
        return jsonObject.toJSONString();
    }

    @RequestMapping("checkUnselectedItem")
    @ResponseBody
    public String checkUnselectedItem(Vendor vendor, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int limit) {
        List<Item> list = relationVendorItemService.selectUnSelectedItem(vendor);
        List<Item> people = relationVendorItemService.selectUnSelectedItemByPage(vendor, page, limit);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("count", list.size());
        jsonObject.put("data", people);
        jsonObject.put("msg", "查询成功");
        return jsonObject.toJSONString();
    }

    @RequestMapping("addItem")
    @ResponseBody
    public String addItem(String vendorId, String idString) {
        JSONObject jsonObject = new JSONObject();
        String[] idList = idString.split(",");
        if (idList == null || idList.length <= 0) {
            jsonObject.put("code", 1);
            jsonObject.put("msg", "未选中产品");
        } else {
            try {
                relationVendorItemService.addItem(vendorId, idList);
                jsonObject.put("code", 0);
                jsonObject.put("msg", "添加成功");
            } catch (Exception e) {
                e.printStackTrace();
                jsonObject.put("code", 1);
                jsonObject.put("msg", "添加失败");
            }
        }
        return jsonObject.toJSONString();
    }

}
